var AddressApp = function() {
	return {
		init : function(){
			AddressApp.validate();
			$("#province").change(function(){
				var provinceId = $(this).val();
				if(provinceId == ""){
					$("#city").html('<option>-请选择城市-</option>');
					$("#area").html('<option>-请选择区域-</option>');
					return;
				}
					
				AddressApp.loadCity(provinceId);
			});
			
			$("#city").change(function(){
				var cityId = $(this).val();
				AddressApp.loadArea(cityId);
			});
			
			var pId = $("#pId").val();
			var cId = $("#cId").val();
			var aId = $("#aId").val();
			if(pId != undefined && pId != ""){
				AddressApp.loadCity(pId,cId);
			}
			
			if(cId != undefined && cId != ""){
				AddressApp.loadArea(cId,aId);
			}
			
		},
		validate:function(){
			// 邮政编码验证   
			jQuery.validator.addMethod("isZipCode", function(value, element) {   
			    var tel = /^[0-9]{6}$/;
			    return this.optional(element) || (tel.test(value));
			}, "请正确填写您的邮政编码");
			
			// 邮政编码验证   
			jQuery.validator.addMethod("select", function(value, element) {   
			    var val = $(element).val();
			    
			    return val != "";
			}, "请选择正确的数据");
			
			// 手机号码验证
			jQuery.validator.addMethod("isMobile", function(value, element) {
			    var length = value.length;
			    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
			    return this.optional(element) || (length == 11 && mobile.test(value));
			}, "请正确填写您的手机号码");
			
			$("#form").validate({
				onfocusout:false,
				onkeyup:false,
				onfocusout:false,
				rules: {
                	name:{"required":true,maxlength:20},
					phone:{"required":true,isMobile:true},
					province:{"required":true},
					city:{"required":true},
					area:{"required":true},
					address:{"required":true,maxlength:60},
					zipCode:{"required":false,maxlength:15}
                },
                messages:{
                	name:{required:'收货人姓名不能为空',maxlength:'收货人姓名不能大于20个字'},
                	phone:{required:'收货人手机不能为空',isMobile:'手机号码格式不正确'},
                	province:{required:"省份不能为空"},
					city:{required:"城市不能为空"},
					area:{required:"区域不能为空"},
					address:{required:"详细地址不能为空",maxlength:"详细地址不能大于60个字"},
					zipCode:{maxlength:"邮编不能大于15位"}
                },
                errorPlacement: function(error, element) {  
                
                },
                showErrors:function(errorMap,errorList) {
                	if(errorList != undefined && errorList.length > 0){
                		var i = errorList[0];
                    	layer.open({
        				    content: i.message,
        				    skin: 'msg',
        				    time: 2 //2秒后自动关闭
        				  });
                	}
                },
                submitHandler:function(form){
                	AddressApp.save();
                }
			})
		},
		save:function(){
			var name = $("#name").val();
			var phone = $("#phone").val();
			var province = $("#province").val();
			var city = $("#city").val();
			var area = $("#area").val();
			var address = $("#address").val();
			var zipCode=$("#zipCode").val();
			var id = $("#id").val();
			var isDefault = $("#isDefault").prop('checked');
			if(isDefault){
				isDefault = 'y';
			}else{
				isDefault = 'n';
			}
			
			var data = {id:id,name:name,phone:phone,provinceId:province,cityId:city,areaId:area,address:address,zipCode:zipCode,isDefault:isDefault};
			var url = $("#APP_ROOT_PATH").val() + 'address/save';
			mui.util.Ajax.asyncGET(url,data,function(res){
				if(res.ret != undefined && res.ret != 0){
					layer.open({
    				    content: res.message,
    				    skin: 'msg',
    				    time: 2 //2秒后自动关闭
    				  });
					return;
				}
				
				if(res){
					if(id != ""){
						window.location.href = $("#APP_ROOT_PATH").val() + 'address/index';
					}else{
						var json = $("#json").val();
						if(json != undefined &&  json != ""){
							window.location.href = $("#APP_ROOT_PATH").val() + 'order/confirm?json=' + json + "&aId=" + res;
						}else{
							var redirectUrl = $("#redirectUrl").val();
							if(redirectUrl != undefined && redirectUrl !=''){
								window.location.href = redirectUrl + "&aId=" + res;
							}else{
								window.location.href =  $("#APP_ROOT_PATH").val() + '/address/index';
							}
						}
					}
				}else{
					layer.open({
    				    content: '收货地址保存失败',
    				    skin: 'msg',
    				    time: 2 //2秒后自动关闭
    				  });
				}
			});
		},
		commit:function(){
			$("#form").submit();
		},
		loadCity:function(provinceId,cityId){
			var data = {provinceId:provinceId};
			var url = $("#APP_ROOT_PATH").val() + '/area/list_city';
			mui.util.Ajax.asyncGET(url,data,function(res){
				var html = '';
				for(var i = 0;i < res.length;i++){
					if(cityId != undefined && cityId == res[i].id){
						html += "<option value='" + res[i].id + "' selected=selected>" + res[i].areaName + "</option>";
					}else{
						html += "<option value='" + res[i].id + "'>" + res[i].areaName + "</option>";
					}
				}
				$("#city").html(html);
				
				if(cityId == undefined || cityId != ""){
					if(res.length > 0){
						AddressApp.loadArea(res[0].id);
					}
				}
			});
		},
		loadArea:function(cityId,areaId){
			var data = {cityId:cityId};
			var url = $("#APP_ROOT_PATH").val() + '/area/list_area';
			mui.util.Ajax.asyncGET(url,data,function(res){
				var html = '';
				for(var i = 0;i < res.length;i++){
					if(areaId != undefined && areaId == res[i].id){
						html += "<option value='" + res[i].id + "' selected=selected>" + res[i].areaName + "</option>";
					}else{
						html += "<option value='" + res[i].id + "'>" + res[i].areaName + "</option>";
					}
				}
				$("#area").html(html);
			});
		},
		cancel:function(id){
			layer.open({
			    content: '您确定删除收货地址吗!',
			    btn: ['确定', '取消'],
			    yes: function(index){
			    	var url = $("#APP_ROOT_PATH").val() + '/address/del';
					var data = {id:id};
					mui.util.Ajax.asyncGET(url,data,function(res){
						if(res.ret != undefined && res.ret != 0){
							layer.open({
		    				    content: res.message,
		    				    skin: 'msg',
		    				    time: 2 //2秒后自动关闭
		    				  });
							return;
						}
						
						if(res){
							window.location.href = $("#APP_ROOT_PATH").val() + '/address/index';
						}else{
							layer.open({
		    				    content: '删除失败',
		    				    skin: 'msg',
		    				    time: 2 //2秒后自动关闭
		    				  });
						}
					});
			    }
			 });
		},
		setDefault:function(id){
			var url = $("#APP_ROOT_PATH").val() + '/address/set_default';
			var data = {id:id};
			mui.util.Ajax.asyncGET(url,data,function(res){
				if(res.ret != undefined && res.ret != 0){
					layer.open({
    				    content: res.message,
    				    skin: 'msg',
    				    time: 2 //2秒后自动关闭
    				  });
					return;
				}
				
				if(res){
					window.location.reload();
				}else{
					layer.open({
    				    content: '设置默认地址失败',
    				    skin: 'msg',
    				    time: 2 //2秒后自动关闭
    				  });
				}
			});	    	
		},
		selectAddress:function(id){
			var json = $("#json").val();
			
			if(json != undefined && json != ""){
				json = encodeURI(json);
				window.location.href = $("#APP_ROOT_PATH").val() + 'order/confirm?json=' + json + "&aId=" + id;
			}else{
				var redirectUrl = $("#redirectUrl").val();
				window.location.href = redirectUrl + "&aId=" + id;
			}
			
		},
		initSelect:function(){
			$("li[name=editAddr]").on('click',function(e){
				e.stopPropagation();
				var id = $(this).parents('ul[name=addrDetail]').attr('id');
				
				var json = $("#json").val();
				if(json != undefined &&  json != ""){
					json = encodeURI(json);
					window.location.href = $("#APP_ROOT_PATH").val() + 'address/detail?id=' + id + '&json=' + json;
				}else{
					var redirectUrl = $("#redirectUrl").val();
					window.location.href = $("#APP_ROOT_PATH").val() + 'address/detail?id=' + id +'&redirectUrl=' + redirectUrl;
				}
			});
			
			$("ul[name=addrDetail]").on('click',function(e){
				var id = $(this).attr('id');
				AddressApp.selectAddress(id);
			});
		}
	}
}();